load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
load("//bazel:defs.bzl", "rust_bench", "rust_ic_test")

package(default_visibility = ["//visibility:public"])

DEPENDENCIES = [
    # Keep sorted.
    "//rs/canonical_state",
    "//rs/canonical_state/tree_hash",
    "//rs/config",
    "//rs/crypto/sha2",
    "//rs/crypto/tree_hash",
    "//rs/interfaces",
    "//rs/interfaces/certified_stream_store",
    "//rs/interfaces/state_manager",
    "//rs/monitoring/logger",
    "//rs/monitoring/metrics",
    "//rs/protobuf",
    "//rs/registry/routing_table",
    "//rs/registry/subnet_type",
    "//rs/replicated_state",
    "//rs/state_layout",
    "//rs/sys",
    "//rs/tree_deserializer",
    "//rs/types/base_types",
    "//rs/types/types",
    "//rs/types/wasm_types",
    "//rs/utils",
    "//rs/utils/thread",
    "//rs/utils/validate_eq",
    "@crate_index//:bit-vec",
    "@crate_index//:crossbeam-channel",
    "@crate_index//:hex",
    "@crate_index//:nix",
    "@crate_index//:parking_lot",
    "@crate_index//:prometheus",
    "@crate_index//:prost",
    "@crate_index//:rand",
    "@crate_index//:rand_chacha",
    "@crate_index//:scoped_threadpool",
    "@crate_index//:serde",
    "@crate_index//:serde_bytes",
    "@crate_index//:slog",
    "@crate_index//:tempfile",
    "@crate_index//:uuid",
]

MACRO_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:strum_macros",
]

DEV_DEPENDENCIES = [
    # Keep sorted.
    "//packages/ic-error-types",
    "//rs/test_utilities/consensus",
    "//rs/test_utilities/logger",
    "//rs/test_utilities/state",
    "//rs/test_utilities/tmpdir",
    "//rs/test_utilities/types",
    "//rs/types/management_canister_types",
    "@crate_index//:assert_matches",
    "@crate_index//:maplit",
    "@crate_index//:proptest",
    "@crate_index//:strum",
]

MACRO_DEV_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:test-strategy",
]

BIN_DEPENDENCIES = [
    # Keep sorted.
    "//packages/ic-error-types",
    "//rs/canonical_state",
    "//rs/canonical_state/certification_version",
    "//rs/canonical_state/tree_hash",
    "//rs/canonical_state/tree_hash/test_utils",
    "//rs/criterion_time",
    "//rs/crypto/tree_hash",
    "//rs/registry/subnet_type",
    "//rs/replicated_state",
    "//rs/test_utilities/state",
    "//rs/test_utilities/types",
    "//rs/types/base_types",
    "//rs/types/types",
    "@crate_index//:criterion",
    "@crate_index//:maplit",
]

rust_library(
    name = "state_manager",
    srcs = glob(["src/**"]),
    crate_features = select({
        "//bazel:malicious_code_enabled": ["malicious_code"],
        "//conditions:default": [],
    }),
    crate_name = "ic_state_manager",
    proc_macro_deps = MACRO_DEPENDENCIES,
    version = "0.9.0",
    deps = DEPENDENCIES,
)

rust_test(
    name = "state_manager_test",
    timeout = "long",
    crate = ":state_manager",
    proc_macro_deps = MACRO_DEV_DEPENDENCIES,
    deps = DEV_DEPENDENCIES,
)

rust_ic_test(
    name = "state_manager_integration",
    timeout = "long",
    srcs = [
        "tests/common/mod.rs",
        "tests/state_manager.rs",
    ],
    crate_root = "tests/state_manager.rs",
    proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
    deps = [":state_manager"] + DEPENDENCIES + DEV_DEPENDENCIES + [
        # Keep sorted.
        "//rs/registry/subnet_features",
        "//rs/state_machine_tests",
        "//rs/test_utilities/io",
        "//rs/test_utilities/metrics",
    ],
)

rust_bench(
    name = "traversal_bench",
    srcs = ["benches/bench_traversal.rs"],
    deps = [":state_manager"] + BIN_DEPENDENCIES,
)
